package com.lyn.gulimall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyn.common.utils.PageUtils;
import com.lyn.common.utils.Query;
import com.lyn.gulimall.order.dao.OrderItemDao;
import com.lyn.gulimall.order.entity.OrderItemEntity;
import com.lyn.gulimall.order.entity.OrderReturnReasonEntity;
import com.lyn.gulimall.order.service.OrderItemService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.Map;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues:声明需要监听的所有队列
     *
     * org.springframework.amqp.core.Message
     *
     * 参数可以写以下类型
     * 1.Message message:原生消息详细详细.头+体
     * 2.T<发送的消息类>OrderReturnReasonEntity content
     * 3.Channel channel:当前传输数据的通道
     *
     * Queue:可以很多人都来监听.只要收到消息,队列删除消息,而且只能有一个收到此消息
     * 场景:
     *      1)\订单服务启动多个:同一个消息,只能有一个客户端收到
     *      2)\只有一个消息完全处理完,方法结束运行,我们才能接收下个消息
     *
     */
//    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity content, Channel channel){
        byte[] body = message.getBody();
        MessageProperties properties = message.getMessageProperties();
        System.out.println("接收到消息...内容:"+message+"==>内容："+content);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag==>"+deliveryTag);
        try {
            if(deliveryTag%2==0){
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物..."+deliveryTag);
            }else{
                //退货 requeue=false 丢弃 true 发回服务器，服务器重新入队
                channel.basicNack(deliveryTag,false,true);
                System.out.println("没有签收了货物..."+deliveryTag);
            }
        }catch (Exception e){

        }
    }

    @RabbitHandler
    public void recieveMessage2(Message message, OrderReturnReasonEntity content, Channel channel){
        byte[] body = message.getBody();
        MessageProperties properties = message.getMessageProperties();
        System.out.println("接收到消息...内容:"+message+"==>内容："+content);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag==>"+deliveryTag);
        try {
            if(deliveryTag%2==0){
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物..."+deliveryTag);
            }else{
                //退货 requeue=false 丢弃 true 发回服务器，服务器重新入队
                channel.basicNack(deliveryTag,false,true);
                System.out.println("没有签收了货物..."+deliveryTag);
            }
        }catch (Exception e){

        }
    }
}